package org.sunrise.game.game.logic.miner;

import org.sunrise.game.game.annotation.MsgHandlerClass;
import org.sunrise.game.game.annotation.MsgHandlerMethod;
import org.sunrise.game.game.human.HumanObject;
import org.sunrise.game.game.logic.system.GameSystem;
import org.sunrise.game.game.logic.system.MinerSystem;
import org.sunrise.game.game.modules.MinerModule;
import org.sunrise.game.game.proto.MinerProto;
import org.sunrise.game.game.proto.TopicProto;

import java.util.HashMap;
import java.util.Map;

@MsgHandlerClass(packetType = TopicProto.TOPIC.TOPIC_TYPE_MINER_VALUE)
public class MinerMsgHandler {
    @MsgHandlerMethod(packetId = MinerProto.FROM_CLIENT.C2S_SyncData_VALUE)
    public static void syncData(HumanObject humanObject, MinerProto.MC2S_SyncData data) {
        MinerModule module = humanObject.getModule(MinerModule.class);
        if (module == null) return;
        module.syncData(data);
        GameSystem.getSystem(MinerSystem.class).update(humanObject);
    }

    @MsgHandlerMethod(packetId = MinerProto.FROM_CLIENT.C2S_RankList_VALUE)
    public static void rankList(HumanObject humanObject) {
        MinerModule module = humanObject.getModule(MinerModule.class);
        if (module == null) return;
        HashMap<String, Integer> info = GameSystem.getSystem(MinerSystem.class).getInfo();
        MinerProto.MS2C_RankList.Builder newBuilder = MinerProto.MS2C_RankList.newBuilder();
        for (Map.Entry<String, Integer> entry : info.entrySet()) {
            MinerProto.RankInfo.Builder builder = MinerProto.RankInfo.newBuilder();
            builder.setName(entry.getKey()).setLevelIndex(entry.getValue());
            newBuilder.addInfos(builder);
        }
        humanObject.sendMsg(TopicProto.TOPIC.TOPIC_TYPE_MINER_VALUE, MinerProto.FROM_SERVER.S2C_RankList_VALUE,newBuilder);
    }
}